package uk.co.accolade.clash.multicast;

import uk.co.accolade.clash.core.*;
import static uk.co.accolade.clash.core.UserChanges.userChanges;

import java.io.IOException;
import java.net.MulticastSocket;
import java.net.InetAddress;

public class MulticastBroadcastTestHarness {

    private static final Username NEW_USER      = new Username( "newuser" );
    private static final Username OTHER_USER    = new Username( "otheruser" );
    private static final Username MILO          = new Username( "milo" );
    private static final Username JOND          = new Username( "jond" );

    private static final ClashFilePath APP_JS   = new ClashFilePath( "/src/app.js" );
    private static final ClashFilePath CALC_JS  = new ClashFilePath( "/src/calc.js" );
    private static final ClashFilePath ARTICLES = new ClashFilePath( "/articles.html" );
    private static final ClashFilePath SCREEN   = new ClashFilePath( "/css/screen.css" );
    private static final ClashFilePath RENDERER = new ClashFilePath( "/plugins/webcommons/impl/org/isotrak/lavhill/web/widgets/layouts/ContactWidgetXHTMLRenderer.groovy" );
    private static final ClashFilePath CSS      = new ClashFilePath( "/frameworks/grunt/impl/org/isotrak/lavhill/grunt/xhtml/CSS.java" );

    public static void main(String[] args) throws IOException {
        MulticastSocket socket = new MulticastSocket(MulticastCommunicator.SOCKET_PORT);
        InetAddress group = InetAddress.getByName(MulticastCommunicator.GROUP_ADDRESS);
        MulticastCommunicator communicator = new MulticastCommunicator( socket, group );
        communicator.register();

        ChangesRegistry changesRegistry = new ChangesRegistry(new Username("testuser"));
        ChangeHandlerFactory changeHandlerFactory = new ChangeHandlerFactory(changesRegistry, communicator);
        changesRegistry.registerClashHandler( new DebugClashHandler() );

        communicator.addChangeHandlerFactory(changeHandlerFactory);

        if( isListenMode( args ) ) {
            communicator.startListening();
        } else if( isClashMode( args ) ){
            communicator.sendChange( userChanges(NEW_USER, APP_JS, CALC_JS) );
            communicator.sendChange( userChanges(OTHER_USER, APP_JS, CALC_JS) );
            communicator.sendChange( userChanges(MILO, APP_JS) );

            communicator.sendChange( userChanges(NEW_USER, ARTICLES, SCREEN) );
            communicator.sendChange( userChanges(OTHER_USER, ARTICLES, SCREEN) );
            communicator.sendChange( userChanges(MILO, SCREEN) );

            communicator.sendChange( userChanges(JOND, RENDERER, CSS) );
        } else if( isRevertMode( args )) {
            communicator.sendChange( userChanges(NEW_USER, APP_JS) );
            communicator.sendChange( userChanges(OTHER_USER, CALC_JS) );

            communicator.sendChange( userChanges(NEW_USER, SCREEN) );
            communicator.sendChange( userChanges(OTHER_USER, ARTICLES) );

            communicator.sendChange( userChanges(JOND, RENDERER) );
        }
    }

    private static boolean isListenMode( String[] args ) {
        return args.length == 1 && args[0].equals("listen");
    }

    private static boolean isClashMode( String[] args ) {
        return args.length == 1 && args[0].equals("clash");
    }

    private static boolean isRevertMode( String[] args ) {
        return args.length == 1 && args[0].equals("revert");
    }
}